package com.yuan.model_community.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityParking;
import com.yuan.model_community.repository.CommunityParkingDao;
import com.yuan.model_community.service.CommunityParkingService;
import com.yuan.model_community.service.dto.CommunityParkingDto;
import com.yuan.model_community.service.mapstruct.CommunityParkingMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class CommunityParkingServiceImpl implements CommunityParkingService {

    private final CommunityParkingDao communityParkingDao;

    private final CommunityParkingMapper communityParkingMapper;

    @Override
    public PageResult<CommunityParkingDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<CommunityParking> page = communityParkingDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        for (CommunityParking communityParking : page) {
//            communityParking.setLiverName(communityParkingDao.findById());
        }
        return PageUtil.toPage(page.map(communityParkingMapper::toDto));
    }

    @Override
    public List<CommunityParkingDto> queryAll() {
        List<CommunityParking> all = communityParkingDao.findAll();
        return communityParkingMapper.toDto(all);
    }

    @Override
    public List<CommunityParkingDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityParking> users = communityParkingDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityParkingMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityParkingDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityParkingDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityParking resources) {
        communityParkingDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CommunityParking resources) throws Exception {
        Optional<CommunityParking> byId = communityParkingDao.findById(resources.getParkId());
        if (byId.isPresent()) {
            BeanUtils.copyProperties(resources, byId.get());
            communityParkingDao.save(byId.get());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityParkingDao.deleteById(ids);
    }
}
